import {message} from 'antd'
import {querySlider, removeSlider, addSlider, editSlider, querySliderItem} from '../services/api';


export default {
  namespace: 'slider',

  state: {
      data: {list: [], pagination: {size: 10, current: 1, total: 0}},
      item:{},
      options: [
          {value:'index',label:'首页轮播'},
          {value:'shop',label:'商城轮播'},
      ],
      loading: false,
      confirmLoading:false,
  },

  effects: {
    *fetch({payload}, { call, put }) {
        yield put({
            type: 'changeLoading',
            payload: true,
        });
        const response = yield call(querySlider, payload);
        yield put({
            type: 'save',
            payload: {pagination: response.page, list: response.data},
        });
        yield put({
            type: 'changeLoading',
            payload: false,
        });
    },
      *fetchItem({payload,callback}, { call, put }) {
          yield put({
              type: 'changeLoading',
              payload: true,
          });
          const response = yield call(querySliderItem, payload);
          yield put({
              type: 'saveItem',
              payload: response.data,
          });
          if(response.status && callback){
             callback()
          }
          yield put({
              type: 'changeLoading',
              payload: false,
          });
      },
      *remove({payload,callback}, { call, put }) {
          yield put({
              type: 'changeLoading',
              payload: true,
          });
          const response = yield call(removeSlider, payload);
          if(response.status){
              message.success('删除成功');
              if(callback){
                callback();
              }
          }else {
              message.success(response.message);
          }
          yield put({
              type: 'changeLoading',
              payload: false,
          });
      },
      *add({payload,callback}, { call, put }) {
          yield put({
              type: 'changeLoading',
              payload: true,
          });
          const response = yield call(addSlider, payload);
          if(response.status){
              message.success('添加成功');
              if(callback){
                  callback();
              }
          }else {
              message.success(response.message);
          }
          yield put({
              type: 'changeLoading',
              payload: false,
          });
      },
      *edit({payload,callback}, { call, put }) {
          yield put({
              type: 'changeLoading',
              payload: true,
          });
          const response = yield call(editSlider, payload);
          if(response.status){
              message.success('编辑成功');
              if(callback){
                  callback();
              }
          }else {
              message.success(response.message);
          }
          yield put({
              type: 'changeLoading',
              payload: false,
          });
      },
    // *fetchSalesData(_, { call, put }) {
    //   const response = yield call(fakeChartData);
    //   yield put({
    //     type: 'save',
    //     payload: {
    //       salesData: response.salesData,
    //     },
    //   });
    // },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        data:payload,
      };
    },
      saveItem(state, { payload }) {
          return {
              ...state,
              item:payload,
          };
      },
    changeLoading(state, { payload }) {
      return {
        ...state,
        loading:payload,
      };
    },
    clear() {
      return {
        visitData: [],
        visitData2: [],
        salesData: [],
        searchData: [],
        offlineData: [],
        offlineChartData: [],
        salesTypeData: [],
        salesTypeDataOnline: [],
        salesTypeDataOffline: [],
        radarData: [],
      };
    },
  },
};
